﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Distribution.Service;
using DistributionApplication.Controls.Events;
using Entities;

namespace DistributionApplication
{
    public partial class frmMeterHome : frmBase
    {
        public frmMeterHome(EPole pole)
        {
            InitializeComponent();
            gridHome.Pole = pole;
        }

        protected override void lblClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        protected override void lblLButton_Click(object sender, EventArgs e)
        {
            frmMeter meterForm = new frmMeter();
            meterForm.Pole = gridHome.Pole;

            List<EMeter> metersPerPole = LMeter.GetAll(gridHome.Pole);
            if (metersPerPole.Count >= Settings.MaxMeterPerPole)
            {
                MessageBox.Show("You have reached the max amount of meters (" + Settings.MaxMeterPerPole + ") per poles.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            Hide();
            meterForm.ShowDialog();
            Show();

            gridHome.ReloadGrid();
        }

        private void grdDistributions_ClickedEditMeter(object sender, GridEventArgs e)
        {
            EMeter meter = LMeter.GetById(e.Id);

            Hide();

            frmMeter meterForm = new frmMeter();
            meterForm.Pole = gridHome.Pole;
            meterForm.MeterToBeLoaded = meter;
            meterForm.ShowDialog();

            Show();

            gridHome.ReloadGrid();
        }

        private void grdDistributions_ClickedRemoveMeter(object sender, GridEventArgs e)
        {
            EMeter selectedMeter = new EMeter();
            selectedMeter.Id = e.Id;

            Hide();
            LMeter.Delete(selectedMeter);
            Show();

            gridHome.ReloadGrid();
        }

        private void frmMeterHome_Load(object sender, EventArgs e)
        {
            Title = "PPI | Meters";

            RButtonText = "Cancel";
            gridHome.ConfigureGrid();

            LButtonText = "New";
        }

        protected override void lblRButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}